<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 系统权限控制器
 * @package App\Http\Controllers\Admin
 */
class SysPermissionController extends BaseController
{
    private $permSer;
    public function __construct()
    {
        parent::__construct();
        $this->permSer = new \App\Services\SysPermissionService();
    }

    /**
     * 首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $permList = $this->permSer->getAllList();
            foreach ($permList as $k => $v){
                if (isset($v['is_public']) && $v['is_public'] == 1){
                    $v['display_name'] .= '    <font color="#006400">(公共)</font>';
                }
                $permList[$k] = $v;
            }
            //树结构用户组列表
            $tree          = new \App\Services\TreeService('perm_id', 'parent_id', 'children');
            $tree->nameKey = 'display_name';
            $tree->load($permList);
            return $tree->makeJsTreeFormat();
        }
        return view('admin.syspermission.index', $this->viewData);
    }
    /**
     * 获取单条权限数据
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function get(Request $request)
    {
        try {
            $id   = $request->id ?? 0;
            $data = $this->permSer->getData($id);

            return $this->showMsg('success', '获取成功', $data);
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 保存、添加权限
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminSysPermissionPost $request)
    {
        try {
            $rs = $this->permSer->saveData($request);
            if ($rs) {
                return $this->showMsg('success', ($request->perm_id ? '编辑' : '添加') . '成功', [], 'admin.syspermission.index');
            }
            throw new \Exception(($request->perm_id ? '编辑' : '添加') . '权限规则失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 保存排序数据
     * @return $this|\Illuminate\Http\Response
     */
    public function saveSort(Request $request)
    {
        try {
            $permId = (int) $request->input('id', 0);
            $parentId = (int) $request->input('parent', 0);
            is_numeric($parentId) || $parentId = 0;
            $orderNum = $request->input('position', 0);
            if (empty($permId)) {
                throw new \Exception('请选择要移动的对象');
            }
            $rs = $this->permSer->setOrder($parentId, $permId, $orderNum);
            if ($rs) {
                return $this->showMsg('success', '移动成功');
            }
            throw new \Exception('移动失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除
     * @return $this|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $ids   = $request->ids ?? 0;
            if (!$ids){
                throw new \Exception('请指定要删除的权限');
            }
            $rs = $this->permSer->del($ids);
            if ($rs){
                return $this->showMsg('success', '删除成功');
            }
            throw new \Exception('删除失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
